Repository navigation
feat(vm): optimize account energy usage window reset (TIP-945) - #7119
yanghang8612 merged 1 commit into
Conversation
utafrali
left a comment
There was a problem hiding this comment.
The logic change is well-reasoned and correctly gated behind the feature flag, with solid test coverage for V1/V2 paths and boundary conditions. The main practical concern is the double allowTvmResourceOptimization() DB read per method call; the other two comments are clarifications around an edge-case boundary and a non-obvious test assertion.
| // If area merging happened during suicide, use the current window size | ||
| long newSize = mergedSize == currentSize ? size : currentSize; | ||
| boolean areaMerged = dynamicPropertiesStore.allowTvmResourceOptimization() | ||
| ? currentUsage > mergedUsage || mergedSize != currentSize |
There was a problem hiding this comment.
allowTvmResourceOptimization() is called twice per method invocation (once for areaMerged, once for the guard before checkResetEnergyUsage). If this method reads from the dynamic properties store (a DB-backed map), that is two unnecessary lookups per call. Cache it as a local boolean at the top of each method:
boolean isOptimizationAllowed = dynamicPropertiesStore.allowTvmResourceOptimization();
boolean areaMerged = isOptimizationAllowed
? currentUsage > mergedUsage || mergedSize != currentSize
: mergedSize != currentSize;
...
if (isOptimizationAllowed) {
checkResetEnergyUsage(newUsage);
}Same applies in resetAccountUsageV2.
There was a problem hiding this comment.
Good catch — cached dynamicPropertiesStore.allowTvmResourceOptimization() in a local boolean allowOptimization variable in both resetAccountUsage and resetAccountUsageV2 to avoid the redundant lookup. Updated in c598184.
| long maxUsage = totalEnergyCurrentLimit <= 0 ? 0L | ||
| : (totalEnergyCurrentLimit > Long.MAX_VALUE / 2 | ||
| ? Long.MAX_VALUE : totalEnergyCurrentLimit * 2); | ||
| if (newUsage > maxUsage) { |
There was a problem hiding this comment.
When totalEnergyCurrentLimit <= 0, maxUsage is set to 0L, which means any newUsage > 0 immediately throws. This could be a subtle footgun if the store ever returns 0 (e.g. during early initialization or a misconfiguration). Consider whether the intent is really "no limit" (skip the check) or "all usage is invalid" (throw). If the intent is a safety guard, a comment explaining the invariant that makes totalEnergyCurrentLimit <= 0 impossible in practice would prevent future confusion. Otherwise, guard with a no-op:
if (totalEnergyCurrentLimit <= 0) {
return; // limit not yet set, skip check
}There was a problem hiding this comment.
Keeping maxUsage = 0L when totalEnergyCurrentLimit <= 0 is intentional here:
TOTAL_ENERGY_CURRENT_LIMITis always initialized at genesis (50,000,000,000Lby default; reading an uninitialized entry throwsIllegalArgumentException), so it cannot silently default to0during startup.- If governance explicitly sets
TOTAL_CURRENT_ENERGY_LIMITto0(or if a non-positive value were ever encountered), keepingmaxUsage = 0Lpreserves the literal2 * totalEnergyCurrentLimitbound (2 * 0 = 0) rather than bypassing the check or multiplying a negative value.
|
|
||
| for (long initialUsage : new long[]{overLimitUsage, cap, withinCapUsage}) { | ||
| setEnergyAccount(CALLER, 10_000L * TRX_PRECISION, initialUsage, 20_000L, | ||
| cancelAllV2 == 1); |
There was a problem hiding this comment.
The else branch asserts assertEquals(initialUsage, account(CALLER).getEnergyUsage()) after finishing a trace that runs a real contract. This passes only if the trivial bytecode "60015000" leaves CALLER's stored energy usage unchanged by resetAccountUsage. That is a non-obvious postcondition. A short comment explaining why (e.g. the contract consumes no stake-based energy so newUsage rounds back to initialUsage) would make the assertion self-documenting and prevent false confidence if the test helper is reused with a different contract.
There was a problem hiding this comment.
Good point — initialUsage (150_000_000L / 200_000_000L) is well above CALLER's frozen energy limit (from 10,000 TRX), so leftFrozenEnergy is 0 and the "60015000" execution pays its small bill entirely in TRX without consuming staked energy, leaving CALLER's stored energyUsage unchanged at initialUsage. Keeping the test minimal as-is since this fixture only runs that trivial bytecode — thanks for the careful review!
77f9a17 to
47a909a
Compare
Under allowTvmResourceOptimization (TIP-945), refine TransactionTrace.resetAccountUsage and resetAccountUsageV2: - Retain the active window size when energy usage increases during execution (currentUsage > mergedUsage) in addition to window size changes (mergedSize != currentSize). - Validate that the reset energy usage does not exceed 2 * totalEnergyCurrentLimit, throwing ArithmeticException otherwise. Add unit tests in Tip945VmTest covering V1/V2 window reset behavior and upper bound validation.
47a909a to
c598184
Compare
What does this PR do?
Refine account energy usage window reset under
allowTvmResourceOptimization(TIP-945):TransactionTrace.java:resetAccountUsage(V1) andresetAccountUsageV2(V2), evaluateareaMergedascurrentUsage > mergedUsage || mergedSize != currentSizewhendynamicPropertiesStore.allowTvmResourceOptimization()is enabled (preservingmergedSize != currentSizebefore activation).allowTvmResourceOptimization(), validate viacheckResetEnergyUsage(newUsage)thatnewUsagedoes not exceed2 * totalEnergyCurrentLimit, throwingArithmeticExceptionotherwise.Tip945VmTest.java:UNDELEGATERESOURCE, and upper bound validation before and after proposal activation.Why are these changes required?
When energy usage increases during VM execution while the active window size matches the pre-consumed window size, retaining the active window keeps account energy window accounting consistent after proposal activation. Adding the upper bound check under
allowTvmResourceOptimizationguards against out-of-range reset energy usage without affecting pre-proposal behavior or public method signatures.This PR has been tested by:
./gradlew :framework:test --tests "org.tron.core.vm.nativecontract.Tip945VmTest"(14/14 passed)../gradlew :framework:test --tests "org.tron.core.db.TransactionTraceTest"(6/6 passed)../gradlew :framework:checkstyleMain :framework:checkstyleTest :plugins:checkstyleMainandbash .github/scripts/check_math_usage.shpassed with 0 violations.Follow up
None.
Extra details
Strictly gated behind
allowTvmResourceOptimization(TIP-945); pre-proposal behavior remains unchanged.